<template>
  <Modal v-model="isShow"
         footer-hide
         @on-cancel="closeEvent"
         :mask-closable="false"
         :title="title">
    <Form ref="form" :label-width="80" :model="formData" :rules="rules">
      <FormItem label="字典编码" prop="dictCode">
        <Input readonly v-model="formData.dictCode"/>
      </FormItem>
      <FormItem label="编码" prop="code">
        <Input placeholder="请输入编码" v-model="formData.code"/>
      </FormItem>
      <FormItem label="名称" prop="name">
        <Input placeholder="请输入名称" v-model="formData.name"/>
      </FormItem>
      <FormItem label="备注" prop="remark">
        <Input type="textarea" maxlength="1024" show-word-limit :autosize="{minRows: 3}" v-model="formData.remark"/>
      </FormItem>
      <FormItem class="form-bottom-btn">
        <Button @click="submitBtnClick" type="primary">保存</Button>
      </FormItem>
    </Form>
  </Modal>
</template>

<script>
import {saveOrUpdateChildDict} from "@/api/sys/childDict";
import {Message} from "view-ui-plus";
import {RESPONSE_CODE_SUCCESS} from "@/constant/response";

const formData = {
  id: undefined,
  dictCode: '',
  code: '',
  name: '',
  remark: ''
}

export default {
  name: "SysChildDictEdit",
  data() {
    return {
      isShow: false,
      title: "",
      formData: formData,
      rules: {
        dictCode: [
          {required: true, message: '不能为空', trigger: 'blur'},
        ],
        code: [
          {required: true, message: '不能为空', trigger: 'blur'},
        ],
        name: [
          {required: true, message: '不能为空', trigger: 'blur'},
        ],
      }
    }
  },
  methods: {
    show({title, data}) {
      this.title = title
      this.formData = this.$utils.clone(formData)
      if (data) {
        Object.assign(this.formData, data)
      }
      this.isShow = true
    },
    closeEvent() {
      this.formData = this.$utils.clone(formData)
    },
    submitBtnClick() {
      this.$refs.form.validate((valid) => {
        if (!valid) {
          return
        }
        saveOrUpdateChildDict(this.formData).then(res => {
          if (res.code == RESPONSE_CODE_SUCCESS) {
            this.$emit("ok")
            Message.success("保存成功")
            this.isShow = false
          }
        })
      })
    }
  }
}
</script>

<style scoped>

</style>
